package com.yfjin.h264;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.yfjin.h264.camera2.Camera2Helper;
import com.yfjin.h264.camera2.ImageUtil;
import com.yfjin.h264.tcrtc.IRtc;
import com.yfjin.h264.tcrtc.IRtcImpl;
import com.yfjin.h264.tcrtc.RtcStatus;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author jinyf
 */
public class MainActivity extends AppCompatActivity {


    private Camera2Helper mCamera2Helper;
//    private byte[] mY;
//    private byte[] mU;
//    private byte[] mV;
//    private int mWidth;
//    private int mHeight;


    private BlockingQueue<YUVData> mYUVBlockingQueue;
    private ExecutorService mYUVExecutorService;
    private byte[] mNv21;


    private ImageView mPreview;
    private NV21ToBitmap mUtil;
    private boolean isOk = false;
    private boolean isRoomClosed = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPreview = findViewById(R.id.preview);
        mUtil = new NV21ToBitmap(this);

        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isOk = true;

                if (isRoomClosed) {
                    mRtc.startRtc(getApplicationContext(), "david", "david", new RtcStatus() {
                        @Override
                        public void roomClosed() {
                            isRoomClosed = true;
                        }

                        @Override
                        public void onEnterResult(long result) {
                            if (result > 0) {
                                isRoomClosed = false;
                            }
                        }
                    }, null);
                }
            }
        });

        initQueue();

        mCamera2Helper = new Camera2Helper(getApplicationContext());
        mCamera2Helper.setYUVDataCallback((y, u, v, w, h, stride) -> {
//            try {
//                mSemaphore.acquire();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            mY = y;
//            mU = u;
//            mV = v;
//            mWidth = w;
//            mHeight = h;
//            mRtcScene.sendYUV(y, u, v, w, h, stride);
//            mSemaphore.release();
            mYUVBlockingQueue.add(new YUVData(y, u, v, w, h, stride));
        });

        mCamera2Helper.start(null);

    }

    private void initQueue() {
        mYUVBlockingQueue = new LinkedBlockingQueue<>();
        mYUVExecutorService = Executors.newSingleThreadExecutor();
        mYUVExecutorService.submit(() -> {
            while (true) {
                try {
                    YUVData data = mYUVBlockingQueue.take();
                    if (mNv21 == null) {
                        int lenght = data.stride * data.height * 3 / 2;
//                        int lenght = data.y.length+data.v.length;
                        mNv21 = new byte[lenght];
                    }
//                    ImageUtil.yuvToNv21(data.y, data.u, data.v, mNv21, data.stride, data.height);
                    ImageUtil.yuv888ToNv21(data.y, data.u, data.v, mNv21, data.stride, data.height);
                    //TODO

                    if (isOk) {
                        //模拟远程传输NV21数据
                        toPreview(mNv21, data.width, data.height);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    private IRtc mRtc = new IRtcImpl();


    private void toPreview(byte[] mNv21, int width, int height) {
        mRtc.sendVideoData(mNv21,width,height);
        //显示
//        Bitmap bitmap = mUtil.nv21ToBitmap(mNv21, width, height);
//        runOnUiThread(() -> mPreview.setImageBitmap(bitmap));
    }

    private static class YUVData {
        int width;
        int height;
        int stride;
        byte[] y;
        byte[] u;
        byte[] v;

        public YUVData(byte[] y, byte[] u, byte[] v, int width, int height, int stride) {
            this.y = y;
            this.u = u;
            this.v = v;
            this.width = width;
            this.height = height;
            this.stride = stride;
        }
    }
}
